/*******************************************************************************
 * Copyright (c) 2010, 2015 git@git.oschina.net:kaiwill/springstage.git
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 *******************************************************************************/
package com.qinyeit.serviceapp.entity.system;

import com.qinyeit.serviceapp.entity.MerchantGroupBaseEntity;
import com.qinyeit.serviceapp.entity.enums.RoleType;
import com.qinyeit.serviceapp.entity.enums.State;
import com.qinyetech.springstage.core.bind.RejectRequestParameter;

import javax.management.relation.Role;
import javax.persistence.*;
import javax.validation.constraints.NotEmpty;
import java.util.Set;

/** 
 * ClassName: ManagementRole <br/>
 * Function: 系统角色<br/> 
 * date: 2015年10月6日 下午2:53:13 <br/> 
 * 
 * @author wuqing 
 * @version  
 * @since JDK 1.7 
 */
@Entity
@Table(name="management_roles")
public class ManagementRole extends MerchantGroupBaseEntity<Long> {

    public static final String ADMINISTRATOR_CODE="administrator"; //超级管理员角色编码

	@Column(name="name")
	@NotEmpty(message = "名称不能为空")
	private String name;//角色名称
	
	@Column(name="code",unique=true)
	@RejectRequestParameter //请求参数不能为它赋值
	private String code;//角色编码

	@Column(name="description")
	private String description;//角色描述
	
	@Column(name="delete_able")
	private boolean deleteAble=true; //是否可以被删除,默认可以删除

	//角色类型
	@Column(name="type")
	private RoleType type = RoleType.CHAINROLE; //角色类型


//	//部门id
//	@Column(name="attach_department_id")
//	private Long attachDepartmentId = null;
//
//	//区域id
//	@Column(name="attach_district_id")
//	private Long attachDistrictId = null;
//
//
//	//数据权限串
//	@Column(name="attach_data_permission")
//	private String attachDataPermission = "";

//	@Transient
//	private String attachDepartmentName = "";
//
//	@Transient
//	private String attachDistrictName = "";


	// 状态
	@Column(name="state", length = 300)
	@Enumerated(EnumType.STRING)
	private State state = State.NORMAL;



	@ManyToMany(cascade={CascadeType.PERSIST},fetch = FetchType.EAGER)
	@JoinTable(
			   name="management_role_permissions",
			   joinColumns={@JoinColumn(name="management_role_id",referencedColumnName="id")},
			   inverseJoinColumns={@JoinColumn(name="management_permission_id",referencedColumnName="id")}
			  )
	private Set<ManagementPermission> permissions; //权限集合

	// 操作人名称
	@Column(name="operator_name", length = 300)
	private String operatorName = null;

	public String getOperatorName() {
		return operatorName;
	}

	public void setOperatorName(String operatorName) {
		this.operatorName = operatorName;
	}

	public boolean isDeleteAble() {
		return deleteAble;
	}

	public void setDeleteAble(boolean deleteAble) {
		this.deleteAble = deleteAble;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public String getCode() {
		return code;
	}

	public void setCode(String code) {
		this.code = code;
	}

	public String getDescription() {
		return description;
	}

	public void setDescription(String description) {
		this.description = description;
	}

	public Set<ManagementPermission> getPermissions() {
		return permissions;
	}

	public void setPermissions(Set<ManagementPermission> permissions) {
		this.permissions = permissions;
	}

	

	@Override
	public boolean equals(Object obj) {
		if (this == obj)
			return true;
		if (!super.equals(obj))
			return false;
		if (getClass() != obj.getClass())
			return false;
		ManagementRole other = (ManagementRole) obj;
		if (this.getId() == null) {
			if (other.getId() != null)
				return false;
		} else if (!this.getId().equals(other.getId()))
			return false;
		return true;
	}

	public State getState() {
		return state;
	}

	public void setState(State state) {
		this.state = state;
	}

	public RoleType getType() {
		return type;
	}

	public void setType(RoleType type) {
		this.type = type;
	}
}
